// miniprogram/pages/home/vitae/input.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTopTips: false,
    msg: "正在加载",
    avatarUrl: './user-unlogin.png',
    sex: ["男", "女"],
    sexIndex: 0,
    birthday: "请选择",
    intro: "",
    introCount: 0,
    files: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  bindSexChange: function(e) {
    var that = this
    that.setData({
      sexIndex: e.detail.value,
    })
  },
  bindDateChange: function(e) {
    this.setData({
      birthday: e.detail.value
    })
  },
  introInput: function handleInput(e) {
    this.setData({
      intro: e.detail.value,
      introCount: e.detail.value.length
    })
  },
  chooseAvatarUrl: function() {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        that.setData({
          avatarUrl: res.tempFilePaths
        })
      },
    })
  },
  chooseImage: function(e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        })
      },
    })
  },
  previewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  deleteImage:function(e){
    var that = this 
    console.log(e)
    var index = parseInt(e.currentTarget.dataset.index);
    that.setData({
      files: that.data.files.splice(index+1,1)
    })

  },
  formSubmit: function(e) {
    var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
    var msg = ""
    var that = this
    let {
      title,
      phone,
      email,
      address
    } = e.detail.value;
    var sex = that.data.sex[that.data.sexIndex];
    var birthday = that.data.birthday;
    var intro = that.data.intro;
    var introCount = that.data.introCount
    var files = that.data.files;
    var avatarUrl = that.data.avatarUrl
    if (address == "" || title == "" || introCount == 0 || birthday == "请选择" || avatarUrl =="./user-unlogin.png") {
      if (title == "") {
        msg = msg + "简历名称 "
      }
      if (address == "") {
        msg = msg + "高中所在地 "
      }
      if (introCount == 0) {
        msg = msg + "自我介绍 "
      }
      if(birthday =="请选择"){
        msg = msg+"出生日期 "
      }
      if (avatarUrl == "./user-unlogin.png"){
        msg = msg + "简历头像 "
      }
      that.setData({
        msg: msg + "不能为空",
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone)) || !reg.test(email)) {
      if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
        msg = msg + "手机号 "
      }
      if (!reg.test(email)) {
        msg = msg + "邮箱 "
      }
      that.setData({
        msg: msg + "输入不合法",
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else if(introCount<10){
      that.setData({
        msg: msg + "自我介绍少于10个字",
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      var files = JSON.stringify(this.data.files)
      wx.navigateTo({
        url: './vitae?title=' + title +
          '&avatarUrl=' + avatarUrl +
          '&sex=' + sex +
          '&phone=' + phone +         
          '&email=' + email +         
          '&address=' + address +
          '&birthday=' + birthday +
          '&intro=' + intro +
          '&files=' + files +
          '&isPreview=1'         
      })
    }

    console.log("phone:" + phone + " email:" + email + " address" + address + " sex:" + sex + " birthday" + birthday + " intro:" + intro + " files:" + files);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})